<template>
    <div>
        <h2>会员添加</h2>
        <table>
            <tbody>
                <tr>
                    <td align="right">会员编号</td>
                    <td>
                        <input type="text" v-model="member.MemberCode" disabled>
                    </td>
                </tr>

                <tr>
                    <td align="right">姓名</td>
                    <td>
                        <input type="text" v-model="member.UserName">
                    </td>
                </tr>

                <tr>
                    <td align="right">手机号</td>
                    <td>
                        <input type="text" maxlength="11" v-model="member.Phone">
                    </td>
                </tr>

                <tr>
                    <td align="right">身份证号</td>
                    <td>
                        <input type="text" maxlength="18" v-model="member.Card">
                    </td>
                </tr>

                <tr>
                    <td align="right">会员等级</td>
                    <td>
                        <select name="" id="" v-model="member.LevelId">
                            <option value="">请选择</option>
                            <option :value="item.Id" v-for="item in data.list" :key="item.Id">{{item.LevelName}}</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td align="right">会员生日</td>
                    <td>
                        <input type="date" v-model="member.Birth">
                    </td>
                </tr>

                <tr>
                    <td align="right">性别</td>
                    <td>
                        <input type="radio" name="sex" :value="1" v-model="member.Sex">男
                        <input type="radio" name="sex" :value="0" v-model="member.Sex">女
                    </td>
                </tr>

                <tr>
                    <td align="right">积分</td>
                    <td>
                        <input type="text" v-model="member.JiFen">
                    </td>
                </tr>

                <tr>
                    <td align="right">余额</td>
                    <td>
                        <input type="text" v-model="member.Money">
                    </td>
                </tr>

                <tr>
                    <td align="right"></td>
                    <td>
                        <input type="button" value="添加" class="btn btn-primary" @click="ok">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive,onMounted} from 'vue'
import axios from 'axios';
import {useRouter} from 'vue-router'
import moment from 'moment';
const router=useRouter();

var num=parseInt((Math.random()*1000).toString())+100

let member=reactive({
  "MemberCode": moment(new Date).format("YYYYMMDD")+num,
  "Phone": "",
  "UserName": "",
  "Card": "",
  "LevelId": "",
  "Birth": "",
  "Sex": 0,
  "JiFen": '',
  "Money": ''
})

const data:any=reactive({
    list:[]
})
onMounted(()=>{
    showlist();
})
const showlist=()=>{
    axios({
        method:"get",
        url:"/api/User/ShowList",
        data:member
    })
    .then(res=>{
        console.log(res);
        data.list=res.data;
    })
    .catch(err=>{
        console.error(err);
    })
}

const ok=()=>{
    //验证
    if(member.UserName==""){
        alert('用户名不能为空');
        return;
    }
    let reg=/^1[3-9]\d{9}$/;
    if(!reg.test(member.Phone)){
        alert('手机号不合法');
        return;
    }
    axios({
        method:'post',
        url:'/api/User/AddMember',
        data:member
    })
    .then((res)=>{
        console.log(res);
        if(res.data>0){
            alert('添加成功');
            router.push('/memberList');
        }
        else if(res.data==-1){
            alert('身份证重复');
            return;
        }
        else if(res.data==-2){
            alert('手机号重复');
            return;
        }
        else{
            alert('添加失败');
        }
    })
    .catch(err=>{
        console.log(err);
    })

}

</script>

<style scoped>

</style>